package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;


    //新增菜品
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("新增成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page>page(int page, int pageSize, String name){
        //构造分页器
        Page<Dish> pageInfo = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name!=null,Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行查询
        dishService.page(pageInfo,queryWrapper);

        //处理分页对象集合中DIsh对象的分类信息
        Page<DishDto> dishDtoPage = new Page<>();
        //将pageinfo分页对象拷贝到dishDtoPage
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        //处理pageinfo分页对象中的records集合数据
        List<Dish> dishList = pageInfo.getRecords();
        List<DishDto> dishDtoList=dishList.stream().map(dish->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            //单独去查询菜品的分类id,对应的分类信息
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //给dishdto设置分类名称
            if(category != null){
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        //将dishDtoLIst集合数据...封装到dishDtoPage对象中
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);

    }


    //删除菜品
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long>ids){
       dishService.removeWithFlavor(ids);
       return R.success("删除成功");
    }


    //根据id查询菜品信息和对应的口味信息
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }



    //修改菜品
    @PutMapping
    public R<String>update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }



    //批量起/停售
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,@RequestParam List<Long>ids){
        dishService.updateStatus(status,ids);
        return R.success("状态修改成功");
    }

    @GetMapping("list")
    public R<String> list(DishDto dishDto) {


        return null;
    }
}
